package com.whj.config;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.whj.entity.Cart;
import com.whj.entity.SysUser;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.List;


//输入账号密码支付
@Component

public class AlipayTemplate {
    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String appId="9021000150649903";
    // 应用私钥，就是工具生成的应用私钥
    public String merchantPrivateKey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCua8K69Nrlz1QO1AICjbP9mue0KqA1qZm497N+QgcTXbAgT+M7GZ7rj6vmqlxMCWk3bXY1MVUUiWWuIYgxJP0LKdk2Bc6gk1HKDE/5GXYKqciIcYoKPlBOMHaZyK/kDo9Y4wumJxp670UpJJQwu0AOroOQlhId2rfk5ZcZpEl05UDWEtuymH8vSsX73skCa2F+A5Z76SEOEPmXuOJQ5xYm0iPiZAbY3Qtt58/4qulbqV5RwjFMJ0JXa1wFgmNyA5fY9Ww2HSFoFX8xQuR3LaP3WH5H+J6Zjbq2uZes8sw/0Gp0sqUVuefRfXFZtGIc4oBjBe7xPvlzAW+UcPu+eMvAgMBAAECggEAMgI0q1nh7EPUKdA24ox9wXrEWuH6iAUVbUD46BS/fIxJxITtOczpu6DBzE2/SlSf4f0K5uMmhgZS6OC4Nurcqsk2VEayRCi6xWDjzkxihZpzi4QeGRjrmX7L8fAIg/35uahOZTjuhTqwf6ghbRHUtoTAzlmlCjvcv6DnIXv/RNenOYjJ6swj1b0gyo5DW2/XVCpicl5qDkUeIVdKqnIjjePOoAexCqjjQcG0F1IWhAHpT2sPqpnXU92IFeCVfeKyFIG3Wb1+6YzR49fkzz5zngROw4a5JbuA+ix/zJl9Hhco8CsdLQWKAAiRfwrvz+0drKcGm3j4XEzJc8j+zAduUQKBgQDn8OcL353WHDK2h/EAEiSmumf/OHqWkJrimQ4h8DiU2eWUJBvk2dzxLQBgslQ4UyVKnw9PLysTmlr89L1xnjiNvuX0Z1sLaBlNA2bMI5rXG1Avib+CCLMSfa+AOo92WbtOivUV78ixxQsWdB1Z82+Ch1HOYSkAdcnmxg0XpfmB9wKBgQCQSQpbjUYwogJRHPTVDWO6HuXLbI+Bwf9sCg++I5PlxIoET+queAWzaBUIsI7Ud8mFJzmqByTlQ6AaSLaNJe6mHqFawKInQQDTzxDxnxgHlGkfsHoSKXXjs8N2JAwNcyoGDwIAkgaEImVAtspFKVyk9DDhKn/XpSzq9ZaheLHaiQKBgQCjKn3fhHhs33ltn0STYDAYepoemYTj1uae8j1W8r9bN86h4KUe4YTkxVu2qr1EwnlBfZQviGctazEHMsTy4PO4A8uRnAQYiisCWpo+ke2j1bsbNjRbLfOL1o9YDdpOz+FM58RyojT1mkCqkxTy6qZPlRKftQo7IKHgjwM217QnEQKBgFWYbyOW8N/jDJrQIdLOqa39rHd8xLnGu999LI+xDYlOik46ls+/9inTgj7wdxtGoO90MLpUmKfF8ZIGNv4PVb+JBlIPW2MMJhCBk9yzdA7zFVYuXIyvufdGapuIkPndqz2C474w4tq6iPTWO7LB8esNzn2Vt9fXbolI/n52WpR5AoGBAKjDlXeup+1zp4BtVn1plCS+rW6RA7nRmIhERrmr1k+82CTV93SmA1IgH00jrc2STbVOGn+l8J08tjnxYJ+SQ95PJquuFOOmuFlK5UMkNSOYNuUZkjxz22AHOswJPKB5lOeToKuern5qRT91R3eJd5LwGspPOupC+FtyXQ5CCgtL";
    // 支付宝公钥,对应APPID下的支付宝公钥。
    public String alipayPublicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA+LadQGDE1fW4AUnmNbqnyAwmWf1hXNupWoh9+fD/wuwIkABKzKmMo1OYq7/Wu3IaRNfNrFt8Mu5k6tA/AY7Gx6Lr5KjDABnYuaOL7x6zUJYw1tZAU8edFyueF7CVtBnzVHnmsVHVNeND9wOc7TrroEQLDuxbb6h3yNywzRV7HA+hkRxN7YdQFBPC/ZgyZpFnvaeLUi6Jp8gflEuo2GsEZWkiHXEIkHBoMDOY/nSu0B6rJ6ZramfZ/cAHf00+EMmDCH7nMwP8Z56XPO9mkd0KKD5lRzw30S4KgtB+Q0BamrskRVzEWNk2m/C/waxrdu8fUvq2lCP9tAyLoXhhHpxnnQIDAQAB";
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notifyUrl="http://localhost:8082/alipay/notify";
    //同步通知，支付成功，一般跳转到成功页
    public String returnUrl="http://localhost:8082/cart/showCart";
    // 签名方式
    private String signType="RSA2";

    // 字符编码格式
    private String charset="utf-8";

    //订单超时时间
    private String timeout = "1m";
    // 支付宝网关；https://openapi-sandbox.dl.alipaydev.com/gateway.do
    public String gatewayUrl= "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMerchantPrivateKey() {
        return merchantPrivateKey;
    }

    public void setMerchantPrivateKey(String merchantPrivateKey) {
        this.merchantPrivateKey = merchantPrivateKey;
    }

    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getTimeout() {
        return timeout;
    }

    public void setTimeout(String timeout) {
        this.timeout = timeout;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }

    public String pay(List<Cart> carts, SysUser user) throws
            AlipayApiException {

        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new
                DefaultAlipayClient(gatewayUrl, appId, merchantPrivateKey,
                "json", charset, alipayPublicKey, signType);

        //2、创建一个支付请求，并设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);

        JSONObject bizContent = new JSONObject();
        Long id = System.currentTimeMillis();
        bizContent.put("out_trade_no",id );  // 我们自己生成的订单编号
        Double sum=0.0;
        Double ssum=0.0;
        for (Cart cart : carts) {
            sum=cart.getGoodsPrice()*cart.getNum();
            ssum=sum+ssum;
        }
        bizContent.put("total_amount", ssum); // 订单的总金额
        bizContent.put("subject", "工资结算 ");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        bizContent.put("body", user.getUserName()+"的账单");
        bizContent.put("timeout_express", "10m");  // 订单的超时时间

        alipayRequest.setBizContent(bizContent.toString());

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应：" + result);
        return result;
    }
}

